class Solution {
public:
    string longestPalindrome(string s) {
        bool isPalindrome[1005][1005]; // 好的习惯是初始化变量
        
        int sLen = s.length();
        for (int i=0; i<sLen-1; i++)
        {
            isPalindrome[i][i] = true;
            isPalindrome[i][i+1] = (s[i] == s[i+1]);
        }
        isPalindrome[sLen-1][sLen-1] = true;
        
		for (int t=2; t<sLen; t++)
		{
			int j = t;
			for (int i=0; j<sLen; i++, j++)
			{
				isPalindrome[i][j] = ((s[i]==s[j]) && isPalindrome[i+1][j-1]);
			}
		}
        
        int res = 1;
        int x = 0;
        for (int i=0; i<sLen; i++)
        {
            for (int j=i+1; j<sLen; j++)
            {
                if (isPalindrome[i][j])
                {
                    if (res < j-i+1)
                    {
                        x = i;
                        res = j-i+1;
                    }
                }
            }
        }
        return s.substr(x, res);
    }
};